﻿namespace Guia.Elements.PatternServants
{
    using System;
    using System.Windows.Automation;
    using Guia.Core;

    /// <summary>
    /// Represents the implementation for the SelectionItem-Pattern for multiple-selection.
    /// </summary>
    public class SelectionItemImpl : AbstractPatternServant, ISelectionItemMulti
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SelectionItemImpl"/> class. 
        /// </summary>
        /// <param name="uiElement">
        /// The UiElement for which the servant provides functionality.
        /// </param>
        public SelectionItemImpl(UiElement uiElement) : base(uiElement)
        {
        }

        #region ISelectionItemMulti Members

        /// <summary>
        /// Gets or sets a value indicating whether this item is selected or not.
        /// If multiple selection is allowed, this selection causes that only this item may be selected. Thus, other selected items may be unselected. Expect this item doesnt change its value throught the assignment.
        /// </summary>
        /// <exception cref="InvalidOperationException">This exception is thrown when this item can't be selected (e.g. because it is not enabled).</exception>
        public bool IsSelected
        {
            get
            {
                return this.UiElement.GetPattern<SelectionItemPattern>().Current.IsSelected;
            }

            set
            {
                if (this.IsSelected && !value)
                {
                    this.Unselect();
                }
                else if (!this.IsSelected && value)
                {
                    this.Select();
                }
            }
        }

        /// <summary>
        /// Selects this item.
        /// If multiple selection is allowed, this selection causes that only this item is selected. Thus, other selected items may become unselected.
        /// </summary>
        /// <exception cref="InvalidOperationException">This exception is thrown when this item can't be selected (e.g. because it is not enabled).</exception>
        public void Select()
        {
            try
            {
                this.UiElement.GetPattern<SelectionItemPattern>().Select();
            }
            catch (ElementNotEnabledException)
            {
                throw new OperationNotSupportedException("The item is not enabled");
            }
        }

        /// <summary>
        /// Unselects this item.
        /// </summary>
        /// <exception cref="InvalidOperationException">This exception is thrown when this item can't be selected/unselected (e.g. because it is not enabled).</exception>
        public void Unselect()
        {
            try
            {
                this.UiElement.GetPattern<SelectionItemPattern>().RemoveFromSelection();
            }
            catch (ElementNotEnabledException)
            {
                throw new OperationNotSupportedException("The item is not enabled");
            }
        }

        /// <summary>
        /// Adds this item to the selection of its parent.
        /// </summary>
        /// <exception cref="InvalidOperationException">An InvalidOperationException is thrown, when this addition to the selection is not supported (e.g. because multi-select is not supported).</exception>
        public void AddToSelection()
        {
            this.UiElement.GetPattern<SelectionItemPattern>().AddToSelection();
        }

        #endregion
    }
}